part of '../../repository.dart';

const _kGooglePlayOneTimePurchasedItemTransactionPromotionalKey = 'promotional';

class _GooglePlayOneTimePurchasedItemTransactionDto extends Equatable
    implements _GooglePlayTransactionDto {
  const _GooglePlayOneTimePurchasedItemTransactionDto._internal({
    required this.orderId,
    required this.timeDto,
    required this.promotional,
    required this.revocationInfoDto,
  });

  @override
  final String orderId;

  @override
  final ApiTimeDto timeDto;

  @override
  final _GooglePlayTransactionRevocationInfoDto? revocationInfoDto;

  final bool promotional;

  @override
  List<Object?> get props {
    return [
      orderId,
      timeDto,
      promotional,
      revocationInfoDto,
    ];
  }

  factory _GooglePlayOneTimePurchasedItemTransactionDto.fromJson(Map<String, dynamic> json) {
    final timeString = json[_kGooglePlayTransactionTimeKey] as String;
    final timeDto = ApiTimeDto.fromString(timeString);

    final revocationInfoJson =
        json[_kGooglePlayTransactionRevocationInfoKey] as Map<String, dynamic>?;

    _GooglePlayTransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoJson != null) {
      revocationInfoDto = _GooglePlayTransactionRevocationInfoDto.fromJson(revocationInfoJson);
    }

    return _GooglePlayOneTimePurchasedItemTransactionDto._internal(
      orderId: json[_kGooglePlayTransactionOrderIdKey],
      timeDto: timeDto,
      promotional: json[_kGooglePlayOneTimePurchasedItemTransactionPromotionalKey],
      revocationInfoDto: revocationInfoDto,
    );
  }

  @override
  GooglePlayOneTimePurchasedItemTransaction toEntity() {
    return GooglePlayOneTimePurchasedItemTransaction(
      orderId: orderId,
      promotional: promotional,
      time: timeDto.toEntity(),
      revocationInfo: revocationInfoDto?.toEntity(),
    );
  }
}
